﻿using DevComponents.DotNetBar;
using Kimd.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WorkStation.Common
{
    [Serializable]
    public class SystemConfig : SingletonProvider<SystemConfig> 
    {
        /// <summary>
        /// 0-sC_UpDown,
        /// 1-sC_LeftRight
        /// 2-sC_Input
        /// 3-sC_Output
        /// 4-sC_Axis
        /// </summary>
        public SerializableDictionary<string, List<int>> ModuleSplitDistance { get; set; }
        public List<string> CardConfig { get; set; }
        public string SystemTheme { get; set; }
        public string ThemeName { get; set; }
        public List<int> ThemeColor { get; set; }
        [XmlIgnore] // Needed because the XmlSerializer can't handle Color objects
        public Color ColorType;
        public string XmlColorType
        {
            get
            {
                return SerializeColor(ColorType);
            }
            set
            {
                ColorType = DeserializeColor(value);
            }
        }
        public int GetProductVacDelay { get; set; } = 500;
        public SerializableDictionary<string, bool> ModuleName2Disable { get; set; }
        public MesParam MesParam { get; set; }
        public TestParam TestParam { get; set; }
        public InUseStatus UsingStatus { get; set; }    
        //吸嘴取放料参数
        public NozzleGetParam FeedTableGrabProductParam;
        public NozzlePutParam FeedTableReleaseroductParam;

        public NozzleGetParam AssembleMirrorGrabProductParam;
        public NozzlePutParam AssembleMirrorReleaseroductParam;

        public NozzleGetParam LeftTransferGrabProductParam;
        public NozzlePutParam LeftTransferReleaseroductParam;

        public NozzleGetParam RightTransferGrabProductParam;
        public NozzlePutParam RightTransferReleaseroductParam;

        public NozzleGetParam UnloadMirrorGrabProductParam;
        public NozzlePutParam UnloadMirrorReleaseroductParam;

        public List<NozzleGetParam> ArmGetPrecisionParam;
        public List<NozzleGetParam> ArmGetTesterParam;

        public List<NozzlePutParam> ArmPutPrecisionParam;
        public List<NozzlePutParam> ArmPutTesterParam;

        public SpotCheckSN SpotSNs;
        public SystemConfig()
        {
            ModuleSplitDistance = new SerializableDictionary<string, List<int>>();
            ModuleName2Disable = new SerializableDictionary<string, bool>();
            MesParam = new MesParam();
            TestParam = new TestParam();
            UsingStatus = new InUseStatus();

            FeedTableGrabProductParam = new NozzleGetParam();
            FeedTableReleaseroductParam = new NozzlePutParam();

            AssembleMirrorGrabProductParam = new NozzleGetParam();
            AssembleMirrorReleaseroductParam = new NozzlePutParam();

            LeftTransferGrabProductParam = new NozzleGetParam();
            LeftTransferReleaseroductParam = new NozzlePutParam();

            RightTransferGrabProductParam = new NozzleGetParam();
            RightTransferReleaseroductParam = new NozzlePutParam();

            UnloadMirrorGrabProductParam = new NozzleGetParam();
            UnloadMirrorReleaseroductParam = new NozzlePutParam();

            ArmGetPrecisionParam = new List<NozzleGetParam>();
            ArmGetTesterParam = new List<NozzleGetParam>();

            ArmPutPrecisionParam = new List<NozzlePutParam>();
            ArmPutTesterParam = new List<NozzlePutParam>();

            SpotSNs = new SpotCheckSN();

            //ArmGetPrecisionParam.Add(new NozzleGetParam());
            //ArmGetPrecisionParam.Add(new NozzleGetParam());
            //ArmGetPrecisionParam.Add(new NozzleGetParam());
            //ArmGetPrecisionParam.Add(new NozzleGetParam());

            //ArmGetTesterParam.Add(new NozzleGetParam());
            //ArmGetTesterParam.Add(new NozzleGetParam());
            //ArmGetTesterParam.Add(new NozzleGetParam());
            //ArmGetTesterParam.Add(new NozzleGetParam());

            //ArmPutPrecisionParam.Add(new NozzlePutParam());
            //ArmPutPrecisionParam.Add(new NozzlePutParam());
            //ArmPutPrecisionParam.Add(new NozzlePutParam());
            //ArmPutPrecisionParam.Add(new NozzlePutParam());

            //ArmPutTesterParam.Add(new NozzlePutParam());
            //ArmPutTesterParam.Add(new NozzlePutParam());
            //ArmPutTesterParam.Add(new NozzlePutParam());
            //ArmPutTesterParam.Add(new NozzlePutParam());
        }
        public void Save()
        {
            XmlSerializerHelper<SystemConfig>.SerializeToFile(this, PathHelper.GetConfigPath("Machine") + "Config.xml");
        }
        public SystemConfig Load()
        {
            return XmlSerializerHelper<SystemConfig>.DeserializeFromFile(PathHelper.GetConfigPath("Machine") + "Config.xml");
        }
        public string SerializeColor(Color color)
        {
            if (color.IsNamedColor)
                return string.Format("{0}:{1}",
                    ColorFormat.NamedColor, color.Name);
            else
                return string.Format("{0}:{1}:{2}:{3}:{4}",
                    ColorFormat.ARGBColor,
                    color.A, color.R, color.G, color.B);
        }
        public Color DeserializeColor(string color)
        {
            byte a, r, g, b;

            string[] pieces = color.Split(new char[] { ':' });

            ColorFormat colorType = (ColorFormat)
                Enum.Parse(typeof(ColorFormat), pieces[0], true);

            switch (colorType)
            {
                case ColorFormat.NamedColor:
                    return Color.FromName(pieces[1]);

                case ColorFormat.ARGBColor:
                    a = byte.Parse(pieces[1]);
                    r = byte.Parse(pieces[2]);
                    g = byte.Parse(pieces[3]);
                    b = byte.Parse(pieces[4]);

                    return Color.FromArgb(a, r, g, b);
            }
            return Color.Empty;
        }
    }
    public enum ColorFormat
    {
        NamedColor,
        ARGBColor
    }
}
